package com.ncloud.uniformInterface.handle;

import com.ncloud.uniformInterface.domain.Result;
import com.ncloud.uniformInterface.exception.BaseException;
import com.ncloud.uniformInterface.utils.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by shiqiang on 2017/6/23.
 */
@ControllerAdvice
public class ExceptionHandle {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof BaseException){
            BaseException base = (BaseException) e;
            e.printStackTrace();
            return ResultUtil.error(base.getCode(),base.getMessage());
        }else if (e instanceof NullPointerException){
            e.printStackTrace();
            return ResultUtil.error("-2","空指针异常！"+e.toString());
        }else{
            e.printStackTrace();
            return ResultUtil.error("-1",e.getMessage());
        }
    }
}
